package com.uphone.edwards.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具
 * Created by yangle on 2016/12/2.
 */
public class TimeUtils {

    /**
     * long时间戳转换成String"yyyy.MM.dd HH:mm"
     * @param time
     * @return String
     */
    public static String dateToString(long time) {
        if(!IsNull.paramsIsNull(time)){
            return dateToString(time, "yyyy.MM.dd HH:mm");
        }else{
            return "";
        }
    }

    /**
     * long时间戳转换成String"yyyy.MM.dd HH:mm:ss"
     * @param time
     * @return String
     */
    public static String dateToStringChat(long time) {
        if(!IsNull.paramsIsNull(time)){
            return dateToString(time, "yyyy.MM.dd HH:mm:ss");
        }else{
            return "";
        }
    }
    /**
     * long时间戳转换成String时间,指定format时间格式
     * @param time
     * @param format
     * @return String
     */
    public static String dateToString(long time, String format) {
        if(!IsNull.paramsIsNull(time) && !IsNull.paramsIsNull(format)){
            Date date = new Date(time);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            return simpleDateFormat.format(date);
        }else{
            return "";
        }
    }

    /**
     * 将String时间转换为String时间,格式'yyyy.MM.dd HH:mm'
     * @param s
     * @return String
     */
    public static String stampToDate(String s){
        String res = "";
        if(!IsNull.paramsIsNull(s)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
            long lt = new Long(s);
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
        }
        return res;
    }
    /**
     * @Title: compareDate  yyyy.MM.dd HH:mm
     * @Description: TODO(日期比较，如果s>=e 返回true 否则返回false)
     * @param s
     * @param e
     * @return boolean
     * @throws @author uguosui
     */
    public static boolean compareDate2(String s, String e) {
        if(!IsNull.paramsIsNull(s) && !IsNull.paramsIsNull(e)) {
            DateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm");
            try {
                Date d1 = df.parse(s);//
                Date d2 = df.parse(e);// 结束
                if (d1.after(d2)) {
                    return true;
                } else {
                    return false;
                }
            } catch (Exception exception) {
                exception.printStackTrace();
                return false;
            }
        }else{
            return false;
        }
    }
    /**
     * 获取当前系统日期
     * @return  String
     */
    public static String getCurrentData(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");// HH:mm
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }

    /**
     * 转化时间,输入时间与当前时间的间隔
     *
     * @param timestamp 传入时间戳
     * @return 返回字符串：时长。 29:36
     */
    public static String converTime(long timestamp) {
        long currentSeconds = System.currentTimeMillis();
        long timeGap = (currentSeconds - timestamp) / 1000;// 与现在时间相差秒数
        String timeStr = null;
        if (timeGap > 60 * 60) {// 1小时-24小时
            if (timeGap / (60 * 60) < 10) {
                if ((timeGap % (60 * 60)) / 60 < 10) {
                    if ((timeGap % (60 * 60)) % 60 < 10) {
                        timeStr = "0" + timeGap / (60 * 60) + ":0" + (timeGap % (60 * 60)) / 60 + ":0" + (timeGap % (60 * 60)) % 60;
                    } else {
                        timeStr = "0" + timeGap / (60 * 60) + ":0" + (timeGap % (60 * 60)) / 60 + ":" + (timeGap % (60 * 60)) % 60;
                    }
                } else {
                    if ((timeGap % (60 * 60)) % 60 < 10) {
                        timeStr = "0" + timeGap / (60 * 60) + ":" + (timeGap % (60 * 60)) / 60 + ":0" + (timeGap % (60 * 60)) % 60;
                    } else {
                        timeStr = "0" + timeGap / (60 * 60) + ":" + (timeGap % (60 * 60)) / 60 + ":" + (timeGap % (60 * 60)) % 60;
                    }
                }
            } else {
                if ((timeGap % (60 * 60)) / 60 < 10) {
                    if ((timeGap % (60 * 60)) % 60 < 10) {
                        timeStr = timeGap / (60 * 60) + ":0" + (timeGap % (60 * 60)) / 60 + ":0" + (timeGap % (60 * 60)) % 60;
                    } else {
                        timeStr = timeGap / (60 * 60) + ":0" + (timeGap % (60 * 60)) / 60 + ":" + (timeGap % (60 * 60)) % 60;
                    }
                } else {
                    if ((timeGap % (60 * 60)) % 60 < 10) {
                        timeStr = timeGap / (60 * 60) + ":" + (timeGap % (60 * 60)) / 60 + ":0" + (timeGap % (60 * 60)) % 60;
                    } else {
                        timeStr = timeGap / (60 * 60) + ":" + (timeGap % (60 * 60)) / 60 + ":" + (timeGap % (60 * 60)) % 60;
                    }
                }
            }
        } else if (timeGap > 60) {// 1分钟-59分钟
            if (timeGap / 60 < 10) {
                if (timeGap % 60 < 10) {
                    timeStr = "0" + timeGap / 60 + ":0" + timeGap % 60;
                } else {
                    timeStr = "0" + timeGap / 60 + ":" + timeGap % 60;
                }
            } else {
                if (timeGap % 60 < 10) {
                    timeStr = timeGap / 60 + ":0" + timeGap % 60;
                } else {
                    timeStr = timeGap / 60 + ":" + timeGap % 60;
                }
            }
        } else {// 1秒钟-59秒钟
            if (timeGap < 10) {
                timeStr = "00:0" + timeGap;
            } else {
                timeStr = "00:" + timeGap;
            }

        }
        return timeStr;
    }
}